package com.android.clinic.doctors.app.ui.inquiry;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.clinic.doctors.app.R;
import com.android.clinic.doctors.app.base.BaseActivity;
import com.android.clinic.doctors.app.entity.ImageViewInfo;
import com.android.clinic.doctors.app.entity.InquiryInfo;
import com.android.clinic.doctors.app.glide.ImageLoaderUtils;
import com.android.clinic.doctors.app.http.HttpClient;
import com.android.clinic.doctors.app.http.base.BaseObserver;
import com.android.clinic.doctors.app.http.utils.RxObservableUtils;
import com.android.clinic.doctors.app.ui.inquiry.view.PageImageView;
import com.android.clinic.doctors.app.utils.LocalDataUtils;
import com.android.clinic.doctors.app.utils.Utils;
import com.jiongbull.jlog.JLog;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 问诊单
 * Created by cheng on 2018/6/13.
 */
public class InquiryBillActivity extends BaseActivity {

    public static final int REQ_CLASSIFY = 9527;
    private InquiryInfo mInquiryInfo = null;
    private EditText etContent;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_inquiry_bill;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        setTitleBarTitle("问诊单");
        mImmersionBar.keyboardEnable(true).navigationBarWithKitkatEnable(false).init();
        TextView tvTime = findViewById(R.id.tv_time);
        TextView tvName = findViewById(R.id.tv_name);
        TextView tvDescription = findViewById(R.id.tv_description);
        ImageView ivHead = findViewById(R.id.iv_patient_head);
        PageImageView pageImageView = findViewById(R.id.page_imageView);
        etContent = findViewById(R.id.et_content);

        mInquiryInfo = this.getIntent().getParcelableExtra("inquiryInfo");

        JLog.e("noticeId: " + mInquiryInfo.getNoticeId());

        tvTime.setText(Utils.getFormatTime(mInquiryInfo.getCreateTime()));
        tvName.setText(mInquiryInfo.getNickName());
        tvDescription.setText(mInquiryInfo.getDescription());
        ImageLoaderUtils.getInstance().loadHeadImage(mInquiryInfo.getImgUrl(), ivHead);
        pageImageView.setVisibility(!Utils.isEmpty(mInquiryInfo.getImgUrl()) ? View.VISIBLE : View.GONE);

        if (!Utils.isEmpty(mInquiryInfo.getImgUrl())) {
            String[] imageArray = mInquiryInfo.getImgUrl().split(",");
            int imgLength = imageArray.length;
            List<ImageViewInfo> dataList = new ArrayList<>();
            for (int i = 0; i < imgLength; i++) {
                ImageViewInfo imageViewInfo = new ImageViewInfo();
                imageViewInfo.setImgUrl(imageArray[i]);
                dataList.add(imageViewInfo);
            }
            LocalDataUtils.objectMap.put(REQ_CLASSIFY, dataList);
        }

        pageImageView.initView(this, 3, 3);
        pageImageView.initData(REQ_CLASSIFY);
    }

    @Override
    protected void widgetClick(View v) {
        super.widgetClick(v);
        if (v.getId() == R.id.btn_reply) {
            String content = etContent.getText().toString().trim();
            if (TextUtils.isEmpty(content)) {
                showToast("回复内容不能为空!");
                return;
            }
            answerInquiry(content);
        }
    }

    /**
     * 医生回复问诊
     * @param content
     */
    private void answerInquiry(String content) {
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("inquireId", mInquiryInfo.getId());
            jsonObject.put("context", content);
            jsonObject.put("userId", mInquiryInfo.getUserId());
            jsonObject.put("doctorId", mInquiryInfo.getDoctorId());
            jsonObject.put("noticeId", mInquiryInfo.getNoticeId());

            MediaType jsonType = MediaType.parse("application/json; charset=utf-8");
            RequestBody body = RequestBody.create(jsonType, jsonObject.toString());

            JLog.e("body:  " + jsonObject.toString());

            HttpClient.getHttpService().answerInquiry(body)
                    .compose(this.bindToLifecycle())
                    .compose(RxObservableUtils.applySchedulers(loading, "正在提交..."))
                    .subscribe(new BaseObserver<String>() {
                        @Override
                        public void onSuccess(String s) {
                            showToast("回复成功!");
                            setResult(RESULT_OK);
                            finish();
                        }

                        @Override
                        public void onFailure(String message) {
                            showToast(message);
                            finish();
                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
